//
//  UserPreferencesViewController.m
//  NavBar
//
//  Created by MaartenG on 26-07-09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UserPreferencesViewController.h"
#import "NavBarAppDelegate.h"
#import "NavBarViewController.h"

@implementation UserPreferencesViewController
@synthesize baseURLTextField, usernameTextField, userpasswordTextField, mainController;

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	
	self.title = [[NSString alloc] initWithString:@"User Preferences"];
	
	UIView *prefView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
	prefView.backgroundColor = UIColorFromRGB(0x003800);
	self.view = prefView;
	
	UILabel *explainLabel =
	[[[UILabel alloc]
	  initWithFrame:
	  CGRectMake(10,243,300,210)]
	 autorelease];
	explainLabel.backgroundColor = UIColorFromRGB(0x003800);
	explainLabel.textColor = UIColorFromRGB(0xCCCCCC);
	explainLabel.font = [UIFont boldSystemFontOfSize:12];
	explainLabel.numberOfLines = 0;
	explainLabel.text = [[NSString alloc] initWithString:@"Welcome to Legend of the Green Dragon, a web-based role playing game. This application allows you to play the game more easily from your phone.\n\nIf you are not a current player, just hit \"Save Preferences\", leaving the login information blank. Select the \"Create a character\" action on the login page and follow the instructions. Add the login data to the LotGD application settings later.\n\nIf you already have an account, fill out your information so the application can automatically log you in. Remember to switch the site skin to \"Jade\" on the login page before using this application."];
	
	
	UILabel *URLLabel =
	[[[UILabel alloc]
	  initWithFrame:
	  CGRectMake(10,10,140,20)]
	 autorelease];
	URLLabel.backgroundColor = UIColorFromRGB(0x003800);
	URLLabel.textColor = UIColorFromRGB(0xCCCCCC);
	URLLabel.font = [UIFont boldSystemFontOfSize:15];
	URLLabel.text = [[NSString alloc] initWithString:@"Game Server URL"];
	
	baseURLTextField = [[UITextField alloc] initWithFrame:CGRectMake(10,30,250,30)];
	baseURLTextField.text = [[NSString alloc] initWithString:@"http://www.lotgd.net/"];
	baseURLTextField.adjustsFontSizeToFitWidth;
	baseURLTextField.borderStyle = UITextBorderStyleRoundedRect;
	baseURLTextField.autocorrectionType = UITextAutocorrectionTypeNo;	
	
	UILabel *usernameLabel =
	[[[UILabel alloc]
	  initWithFrame:
	  CGRectMake(10,70,140,20)]
	 autorelease];
	usernameLabel.font = [UIFont boldSystemFontOfSize:15];
	usernameLabel.text = [[NSString alloc] initWithString:@"Your Username"];
	usernameLabel.backgroundColor = UIColorFromRGB(0x003800);
	usernameLabel.textColor = UIColorFromRGB(0xCCCCCC);
	
	usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(10,90,250,30)];
	usernameTextField.clearsOnBeginEditing = YES;
	usernameTextField.adjustsFontSizeToFitWidth;
	usernameTextField.borderStyle = UITextBorderStyleRoundedRect;
	usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;	
	
	UILabel *userpasswordLabel =
	[[[UILabel alloc]
	  initWithFrame:
	  CGRectMake(10,130,140,20)]
	 autorelease];
	userpasswordLabel.backgroundColor = UIColorFromRGB(0x003800);
	userpasswordLabel.textColor = UIColorFromRGB(0xCCCCCC);
	userpasswordLabel.font = [UIFont boldSystemFontOfSize:15];
	userpasswordLabel.text = [[NSString alloc] initWithString:@"Your Password"];
	
	userpasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(10,150,250,30)];
	userpasswordTextField.clearsOnBeginEditing = YES;
	userpasswordTextField.adjustsFontSizeToFitWidth;
	userpasswordTextField.secureTextEntry = 1;
	userpasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
	userpasswordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
	
	UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[submitButton setTitle:@"Save Preferences" forState:UIControlStateNormal];
	[submitButton setTitleColor:UIColorFromRGB(0x000000) forState:UIControlStateNormal];
	submitButton.frame = CGRectMake( 100, 200, 160, 30);
	[submitButton addTarget:self action:@selector(savePreferences) forControlEvents:UIControlEventTouchUpInside];
	
	// With this, the textFieldShouldReturn method will dismiss the keyboard
	usernameTextField.delegate = self;
	baseURLTextField.delegate = self;
	userpasswordTextField.delegate = self;
	
	[self.view addSubview:explainLabel];
	[self.view addSubview:URLLabel];
	[self.view addSubview:baseURLTextField];	
	[self.view addSubview:usernameLabel];
	[self.view addSubview:usernameTextField];
	[self.view addSubview:userpasswordLabel];
	[self.view addSubview:userpasswordTextField];
	[self.view addSubview:submitButton];
	


}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	
	[textField resignFirstResponder];
	
	return YES;
	
}





- (void)savePreferences {
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	if([self.baseURLTextField.text length] > 0) {	
		[prefs setValue:[NSString stringWithFormat:self.baseURLTextField.text] forKey:@"userBaseURL"];
	}	
	if([self.usernameTextField.text length] > 0){
		[prefs setValue:[NSString stringWithFormat:self.usernameTextField.text] forKey:@"userName"];
	}
	if([self.userpasswordTextField.text length] > 0){	
		[prefs setValue:[NSString stringWithFormat:self.userpasswordTextField.text] forKey:@"userPass"];
	}	
	
	NavBarAppDelegate *delegate = (NavBarAppDelegate *) [ [ UIApplication sharedApplication] delegate];
	[delegate loadPreferences];	
	
	UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[delegate mainController]];
	[navController setNavigationBarHidden:YES];

	self.view = navController.view;
	
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
